package com.lovo.controller;

import com.alibaba.fastjson.JSON;
import com.lovo.bean.SysCommodity;
import com.lovo.config.RedisCacheUtil;
import com.lovo.dto.BuyerDto;
import com.lovo.service.ISysCommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("qd/buyerCar")
public class BuyerCarController {

    @Autowired
    ISysCommodityService commodityService;

    @Autowired
    RedisCacheUtil redisCacheUtil;


    @PutMapping("addBuyerCar/{userId}/{productId}")
    public String addBuyerCar(@PathVariable("userId") Integer userId,
                              @PathVariable("productId") Integer productId) {



        String userKey = String.valueOf(userId);
        String productKey = String.valueOf(productId);
        //查询用户的购物车里面有没有这个商品
        if (redisCacheUtil.hHasKey(userKey, productKey)) {

            BuyerDto buyerDto1 = JSON.parseObject(JSON.toJSONString(redisCacheUtil.hget(userKey, productKey)), BuyerDto.class);
            buyerDto1.setNum(buyerDto1.getNum() + 1);
            redisCacheUtil.hdel(userKey, productKey);
            redisCacheUtil.hset(userKey, productKey, buyerDto1);

        } else {//用户购物车 没有这个商品 新添商品  设置默认数量为1
            BuyerDto buyerDto = getDto(productId);
            buyerDto.setNum(1);
            redisCacheUtil.hset(userKey, String.valueOf(buyerDto.getProductId()), buyerDto);


        }

        return "ok";
    }

    @PutMapping("updateBuyerCarNum/{productId}/{userId}/{num}")
    public String updateBuyerCarNum(@PathVariable("productId") Integer productId,
                                    @PathVariable("userId") Integer userId,
                                    @PathVariable("num") Integer num) {
        String userKey = String.valueOf(userId);
        String productKey = String.valueOf(productId);
        if (redisCacheUtil.hHasKey(userKey, productKey)) {

            BuyerDto buyerDto = JSON.parseObject(JSON.toJSONString(redisCacheUtil.hget(userKey, productKey)), BuyerDto.class);
            buyerDto.setNum(num);
            redisCacheUtil.hdel(userKey, productKey);
            redisCacheUtil.hset(userKey, productKey, buyerDto);
            return "ok";
        }
        return "no";
    }


    @DeleteMapping("delBuyerCar/{userId}/{productId}")
    public String delBuyerCar(@PathVariable("userId") Integer userId,
                              @PathVariable("productId") Integer productId) {

        String userKey = String.valueOf(userId);
        String productKey = String.valueOf(productId);
        redisCacheUtil.hdel(userKey, productKey);
        return "ok";
    }


    @GetMapping("findBuyerCar/{userId}")
    public List<Object> findBuyerCar(@PathVariable("userId") Integer userId) {

        List<Object> buyerDtoList = new ArrayList<>();
        String userKey = String.valueOf(userId);
        Set<Object> set = redisCacheUtil.hashGetFileds(userKey);
        for (Object obj : set) {
            //获得Map的key
            String mapKey = String.valueOf(obj);
            //获取对象
            Object buyerDto =redisCacheUtil.hget(userKey, mapKey);
            buyerDtoList.add(buyerDto);
        }
        return buyerDtoList;

    }

    public BuyerDto getDto(Integer productId) {
        BuyerDto buyerDto = new BuyerDto();
        SysCommodity commodity = commodityService.getById(productId);
        buyerDto.setProductId(commodity.getProductid());
        buyerDto.setPrice(commodity.getPPrice());
        buyerDto.setProductName(commodity.getPName());
        return buyerDto;
    }


}
